import yaml


def updateYaml(host, user, port, password, database='') -> bool:
    try:
        # 加载 YAML 文件
        with open('config/config.yml', 'r') as file:
            data = yaml.load(file, Loader=yaml.FullLoader)

        # 修改数据
        data = {'host': host, 'user': user, 'port': port, 'database': database, 'password': password}

        # 将修改后的数据转为 YAML
        output = yaml.dump(data, default_flow_style=False, allow_unicode=True)
        print(output)
        # 保存到文件
        with open('config/config.yml', 'w') as file:
            file.write(output)
        return True
    except Exception as e:
        print(e)
        return False


def config_info(host, user, port, password, database='') -> bool:
    status: bool = updateYaml(host, user, port, password, database)
    print(status)
    if status:
        print("保存成功！")
    else:
        print("保存失败！")
    return status


if __name__ == "__main__":
    config_info()
